#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#查票
import requests
from json import loads
class Ticket(object):
    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
        }
        self.session = requests.Session()

    def check(self):
        url="https://kyfw.12306.cn/otn/leftTicket/query?" \
            "leftTicketDTO.train_date=2017-12-19&leftTicketDTO.from_station=GZQ&leftTicketDTO.to_station=KMM&purpose_codes=ADULT"

        response = self.session.get(url=url, headers=self.headers)
        dic = loads(response.content)
        for result in dic['data']['result']:
            c = str(result).split('|')
            print("车次:%s;出发地:%s;到达地:%s;出发时间:%s;到达时间:%s,状态%s;历程:%s;商务特等座:%s;一等座:%s;"
                  "二等座:%s;高级软卧:%s;软卧:%s;动卧:%s;硬卧:%s;软座:%s;硬座:%s;无座:%s;其他:%s;备注:%s"
                  %(c[3],c[6],c[7],c[8],c[9],c[11],c[10],"--",c[20],c[21],c[22],c[23],c[24],c[25],c[26],c[27],c[28],c[29],c[1]))
            print("-------------------------------------------------------------------------------------------------------------"
                  "--------------------------------------------------------------------------")

        pass
if __name__=='__main__':
    t=Ticket()
    t.check()
    pass